<?php
include_once("../inc/constants.inc.php");
include_once("../inc/connection.php");
session_start();

if(isset($_POST['questionSubmitButton']) && $_POST['questionSubmitButton']=="Save")
{
	$questionTable = "question_table";
	$correctOption = $_POST['optionRadio'];
	$questionNumber = 1;  //$_POST['hiddenQuestionId'];   //for update and delete check it
	$test_type = $_SESSION['test_type'];
	$question = mysql_real_escape_string($_POST['questionTextArea']);			
	$optionOne = mysql_real_escape_string($_POST['optionOne']);
	$optionTwo = mysql_real_escape_string($_POST['optionTwo']);
	$optionThree = mysql_real_escape_string($_POST['optionThree']);
	$optionFour = mysql_real_escape_string($_POST['optionFour']);
	
	//$questionType = $_POST['hiddenId'];   //$_POST['questionTypeId']; ------------have to change
	//$test_type = $_SESSION['test_type'];
			
		// Validating questions ---- functions.php
	validateQuestion($questionTable, $question, $optionOne, $optionTwo, $optionThree, $optionFour, $test_type, $questionNumber);
		
			if($_POST['optionRadio']=="a")
				{
					$correctAnswer = $optionOne;
				}
			elseif($_POST['optionRadio']=="b")
				{
					$correctAnswer = $optionTwo;
				}
			elseif($_POST['optionRadio']=="c")
				{
					$correctAnswer = $optionThree;
				}
			elseif($_POST['optionRadio']=="d")
				{
					$correctAnswer = $optionFour;
				}	
			
			
			echo $test_type; echo "<br/>";
			echo $question; echo "<br/>";
			echo $correctAnswer; echo "<br/>";
			echo $correctOption; echo "<br/>";
			echo $optionOne;	echo "<br/>";
			echo $optionTwo;	echo "<br/>";
			echo $optionThree;	echo "<br/>";
			echo $optionFour;	echo "<br/>";
			//exit (0);	echo "<br/>";
				
				
				$sqlQuestion = "insert into `question_table`(typeId, question, correctAnswer, correctOption, optionOne, optionTwo, optionThree, optionFour) values('$test_type', '$question', '$correctAnswer', $correctOption,'$optionOne', '$optionTwo', '$optionThree', '$optionFour')";
				
				
				
			if(!mysql_query($sqlQuestion))
			echo "error occurred";
				
				/*
				if(mysql_query($sqlQuestion))
					{
						$url="add-update-question.php?isAdded=yes&selectType=$test_type";
						header("Refresh:0;URL=$url");
					}
					else
					{
						$url="add-update-question.php?isAdded=no";
						header("Refresh:0;URL=$url");
					}
				*/
}


function validateQuestion($questionTable, $questionTextArea, $optionOnefunc, $optionTwofunc, $optionThreefunc, $optionFourfunc, $optionRadio, $questionNumber)
	{
	

		//$_SESSION['adminQuestionType'];
		$typeId = $_SESSION['test_type'];
		
		$questionID = $questionNumber;
	//$questionTable = "question_table";

			$question = addslashes(mysql_real_escape_string($questionTextArea));			
			$optionOne = addslashes(mysql_real_escape_string($optionOnefunc));
			$optionTwo = addslashes(mysql_real_escape_string($optionTwofunc));
			$optionThree = addslashes(mysql_real_escape_string($optionThreefunc));
			$optionFour = addslashes(mysql_real_escape_string($optionFourfunc));

			if(empty($question))
			{
				$url="add-question.php?isEmpty=yes&one=$optionOne&two=$optionTwo&three=$optionThree&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID";			
				header("Refresh:0;URL=$url");
				exit(0);
			}
			elseif(empty($optionOne))
			{
				$url="add-question.php?isEmpty=yes&question=$question&two=$optionTwo&three=$optionThree&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID";					
				header("Refresh:0;URL=$url");
				exit(0);
			}
			elseif(empty($optionTwo))
			{
				$url="add-question.php?isEmpty=yes&question=$question&one=$optionOne&three=$optionThree&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID";					
				header("Refresh:0;URL=$url");
				exit(0);
			}
			elseif(empty($optionThree))
			{
				$url="add-question.php?isEmpty=yes&question=$question&two=$optionTwo&one=$optionOne&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID";					
				header("Refresh:0;URL=$url");
				exit(0);
			}
			elseif(empty($optionOne))
			{
				$url="add-question.php?isEmpty=yes&question=$question&two=$optionTwo&three=$optionThree&one=$optionOne&selectType=$typeId&hiddenQuestionId=$questionID";					
				header("Refresh:0;URL=$url");
				exit(0);
			}
			elseif(empty($optionRadio))
			{
				$url="add-question.php?isEmpty=yes&isEmptyCheck=yes&question=$question&one=$optionOne&two=$optionTwo&three=$optionThree&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID";
				header("Refresh:0;URL=$url");
				exit(0);
			}
						
	}



?>